package com.chucang.shucang.common.base.convert;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.text.CharSequenceUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.convert.converter.Converter;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @author flitsneak
 * @email flitsneak@gmail.com
 * @date 2022/9/11 15:13
 * @description 日期转换类
 */
@Slf4j
@Component
public class DateConverter implements Converter<String, Date> {

    @Override
    public Date convert(String source) {
        String value = source.trim();
        if (CharSequenceUtil.isBlank(value)) {
            return null;
        }
        try {
            return DateUtil.parse(source);
        } catch (IllegalArgumentException e) {
            log.error("数据日期解析异常！" + e.getMessage());
        }
        return null;
    }
}
